﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.Data;
using Billing.Controls;
using System.Windows.Forms.Integration;
using System.Drawing.Printing;
using System.Configuration;

namespace Billing.Windows
{
    public partial class SearchBulkOrders : Window
    {
        DataTable dtSearchGrid = null;
        string bulkBillNO = string.Empty;

        public SearchBulkOrders()
        {
            InitializeComponent();
        }

        private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.F1)
                {
                    byBillNoBrdr.Visibility = Visibility.Visible;
                    byDeliveryDateBrdr.Visibility = Visibility.Collapsed;
                    byPhoneNo.Visibility = Visibility.Collapsed;
                    msg.Text = "Enter Bulk Order Bill No and Press Enter";
                    bulkBillNo.Clear();
                    bulkBillNo.Focus();
                }
                else if (e.Key == Key.F2)
                {
                    byBillNoBrdr.Visibility = Visibility.Collapsed;
                    byDeliveryDateBrdr.Visibility = Visibility.Visible;
                    byPhoneNo.Visibility = Visibility.Collapsed;
                    printGrid.Visibility = Visibility.Collapsed;
                    msg.Text = "Select valid Date and Press G";
                    deliverydatepicker.SetFocusOnDate();
                }
                else if (e.Key == Key.F3)
                {
                    byBillNoBrdr.Visibility = Visibility.Collapsed;
                    byDeliveryDateBrdr.Visibility = Visibility.Collapsed;
                    byPhoneNo.Visibility = Visibility.Visible;
                    printGridByMobile.Visibility = Visibility.Collapsed;
                    msg.Text = "Enter Customer Phone Number and Press Enter";
                    customerPhoneNumber.Clear();
                    customerPhoneNumber.Focus();
                }
                else if (e.Key == Key.Enter && byBillNoBrdr.Visibility == Visibility.Visible && bulkBillNo.Text != string.Empty)
                {
                    DataSet ds = Database.GetBulkOrderDetails(bulkBillNo.Text);

                    bulkBillNo.Tag = bulkBillNo.Text;

                    if (ds != null && ds.Tables.Count == 2)
                    {
                        msg.Text = "Details for Bulk Order Bill # " + bulkBillNo.Tag.ToString().ToUpper() + " are filled below.";
                        bulkBillNO = bulkBillNo.Text;
                        customerName.Text = ds.Tables[0].Rows[0]["CustomerName"].ToString();
                        phoneNo.Text = ds.Tables[0].Rows[0]["PhoneNo"].ToString();
                        bookedDate.Text = DateTime.Parse(ds.Tables[0].Rows[0]["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt");
                        deliveryDate.Text = DateTime.Parse(ds.Tables[0].Rows[0]["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + ds.Tables[0].Rows[0]["DeliveryTime"].ToString();
                        deliveryPlace.Text = ds.Tables[0].Rows[0]["DeliveryPlace"].ToString();
                        totalAmt.Text = ds.Tables[0].Rows[0]["TotalAmount"].ToString();
                        //advance.Text = dt.Rows[0]["Advance"].ToString();
                        //balance.Text = dt.Rows[0]["Balance"].ToString();
                        cakeDelivery.Text = ds.Tables[0].Rows[0]["IsOrderDelivered"].ToString();
                        //extraValue.Content = dt.Rows[0]["ExtraValue"].ToString();
                        //discount.Text = dt.Rows[0]["DiscountByCost"].ToString();
                        advance.Text = ds.Tables[0].Rows[0]["Advance"].ToString();
                        balance.Text = ds.Tables[0].Rows[0]["Balance"].ToString();
                        
                        if (ds.Tables[0].Rows[0]["DiscountByCost"].ToString() != "0")
                        {
                            discount.Text = ds.Tables[0].Rows[0]["DiscountByCost"].ToString();
                            disc.Text = "Discount Amount: ";
                        }
                        else if (ds.Tables[0].Rows[0]["DiscountByPercentage"].ToString() != "0")
                        {
                            discount.Text = ds.Tables[0].Rows[0]["DiscountByPercentage"].ToString();
                            disc.Text = "Discount Percentage: ";
                        }

                        if (ds.Tables[0].Rows[0]["EachBoxCost"].ToString() == "0")
                            eachboxcost.Text = "-";
                        else
                            eachboxcost.Text = ds.Tables[0].Rows[0]["EachBoxCost"].ToString();

                        if (ds.Tables[0].Rows[0]["Qty"].ToString() == "0")
                            qty.Text = "-";
                        else
                            qty.Text = ds.Tables[0].Rows[0]["Qty"].ToString();

                        lbItems.Items.Clear();

                        foreach (DataRow row in ds.Tables[1].Rows)
                        {
                            BulkOrderControl ctrl = new BulkOrderControl();
                            ctrl.lblCode.Content = row["ItemCode"].ToString();
                            ctrl.lblItem.Content = row["ItemName"].ToString();
                            ctrl.lblQty.Content = row["Qty"].ToString();
                            lbItems.Items.Add(ctrl);
                        }

                        printCake.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        msg.Text = "Bulk Order Bill # Doesn't Exists";
                        //customerName.Text = string.Empty;
                        phoneNo.Text = string.Empty;
                        bookedDate.Text = string.Empty;
                        deliveryDate.Text = string.Empty;
                        //shape.Text = string.Empty;
                        //albumModel.Text = string.Empty;
                        //message.Text = string.Empty;
                        //description.Text = string.Empty;
                        //flavour.Text = string.Empty;
                        //qty.Text = string.Empty;
                        totalAmt.Text = string.Empty;
                        //advance.Text = string.Empty;
                        //balance.Text = string.Empty;
                        //extra.Text = string.Empty;
                        cakeDelivery.Text = string.Empty;
                        bulkBillNo.Text = bulkBillNO;
                        //extraValue.Content = string.Empty;
                        //discount.Text = string.Empty;
                        printCake.Visibility = Visibility.Collapsed;
                        //cakebillNo.Clear();
                        //cakebillNo.Focus();
                        lbItems.Items.Clear();
                    }
                }
                else if (e.Key == Key.P && byBillNoBrdr.Visibility == Visibility.Visible)
                {
                    if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                    {
                        NewBulkPrinter form = new NewBulkPrinter(bulkBillNo.Tag.ToString());
                        form.printDocument1.PrintController = new StandardPrintController();
                        form.printDocument1.Print();
                        form.Dispose();
                    }
                    else
                    {
                        OldBulkPrinter form = new OldBulkPrinter(bulkBillNo.Tag.ToString());
                        form.printDocument1.PrintController = new StandardPrintController();
                        form.printDocument1.Print();
                        form.Dispose();
                    }
                    bulkBillNo.Text.Replace("P", string.Empty);
                    bulkBillNo.Text.Replace("p", string.Empty);
                }
                else if (e.Key == Key.G && byDeliveryDateBrdr.Visibility == Visibility.Visible)
                {
                    dtSearchGrid = Database.GetAllBulkOrdersByDeliveryDate(new DateTime(int.Parse(deliverydatepicker.nudYear.txtBoxValue.Text), int.Parse(deliverydatepicker.nudMonth.txtBoxValue.Text), int.Parse(deliverydatepicker.nudDate.txtBoxValue.Text)));
                    lbCakesGrid.Items.Clear();

                    if (dtSearchGrid != null && dtSearchGrid.Rows.Count > 0)
                    {
                        msg.Text = "Details are filled.";
                        printGrid.Visibility = Visibility.Visible;
                        foreach (DataRow row in dtSearchGrid.Rows)
                        {
                            BulkItemControl ctrl = new BulkItemControl();
                            ctrl.lblBillNo.Content = row["BillNo"].ToString();
                            ctrl.lblCustomerName.Content = row["CustomerName"].ToString();
                            ctrl.lblPhoneNo.Content = row["PhoneNo"].ToString();
                            ctrl.lblBookedDate.Content = row["BookedDate"].ToString();
                            ctrl.lblDeliveryDate.Content = row["DeliveryDate"].ToString() + " " + row["DeliveryTime"].ToString();
                            ctrl.lblAmount.Content = row["Amount"].ToString();
                            ctrl.lblAdvance.Content = row["Advance"].ToString();
                            ctrl.lblBalance.Content = row["Balance"].ToString();
                            ctrl.lblDiscountByCost.Content = row["DiscRs"].ToString();
                            ctrl.lblDiscountByPercentage.Content = row["DiscPerc"].ToString();
                            ctrl.lblDelivered.Content = row["IsOrderDelivered"].ToString();
                            lbCakesGrid.Items.Add(ctrl);
                        }
                    }
                    else
                    {
                        msg.Text = "No Details Available for the Selected Criteria.";
                        printGrid.Visibility = Visibility.Collapsed;
                    }
                }
                else if (e.Key == Key.P && byDeliveryDateBrdr.Visibility == Visibility.Visible && printGrid.Visibility == Visibility.Visible)
                {
                    foreach (DataRow row in dtSearchGrid.Rows)
                    {
                        if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                        {
                            NewBulkPrinter form = new NewBulkPrinter(row["BillNo"].ToString());
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                        else
                        {
                            OldBulkPrinter form = new OldBulkPrinter(row["BillNo"].ToString());
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                    }
                }
                else if (e.Key == Key.Enter && byPhoneNo.Visibility == Visibility.Visible && customerPhoneNumber.Text != string.Empty)
                {
                    dtSearchGrid = Database.GetAllBulkOrdersByPhoneNo(customerPhoneNumber.Text);
                    lbCakesGridByMobile.Items.Clear();

                    if (dtSearchGrid != null && dtSearchGrid.Rows.Count > 0)
                    {
                        msg.Text = "Details are filled.";
                        printGridByMobile.Visibility = Visibility.Visible;
                        foreach (DataRow row in dtSearchGrid.Rows)
                        {
                            BulkItemControl ctrl = new BulkItemControl();
                            ctrl.lblBillNo.Content = row["BillNo"].ToString();
                            ctrl.lblCustomerName.Content = row["CustomerName"].ToString();
                            ctrl.lblPhoneNo.Content = row["PhoneNo"].ToString();
                            ctrl.lblBookedDate.Content = row["BookedDate"].ToString();
                            ctrl.lblDeliveryDate.Content = row["DeliveryDate"].ToString() + " " + row["DeliveryTime"].ToString();
                            ctrl.lblAmount.Content = row["Amount"].ToString();
                            ctrl.lblAdvance.Content = row["Advance"].ToString();
                            ctrl.lblBalance.Content = row["Balance"].ToString();
                            ctrl.lblDiscountByCost.Content = row["DiscRs"].ToString();
                            ctrl.lblDiscountByPercentage.Content = row["DiscPerc"].ToString();
                            ctrl.lblDelivered.Content = row["IsOrderDelivered"].ToString();
                            lbCakesGridByMobile.Items.Add(ctrl);
                        }
                    }
                    else
                    {
                        msg.Text = "No Details Available for the Selected Criteria.";
                        printGridByMobile.Visibility = Visibility.Collapsed;
                    }
                }
                else if (e.Key == Key.P && byPhoneNo.Visibility == Visibility.Visible && printGridByMobile.Visibility == Visibility.Visible)
                {
                    foreach (DataRow row in dtSearchGrid.Rows)
                    {
                        if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                        {
                            NewBulkPrinter form = new NewBulkPrinter(row["BillNo"].ToString());
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                        else
                        {
                            OldBulkPrinter form = new OldBulkPrinter(row["BillNo"].ToString());
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                    }
                    customerPhoneNumber.Text = string.Empty;
                }
            }
            catch (Exception) { }
        }
    }
}
